package bb.lanxing.devices.sync;

import com.garmin.fit.Mesg;
import com.garmin.fit.SessionMesg;
import bb.lanxing.lib.devices.core.sync.DeviceFile;
import bb.lanxing.lib.devices.sync.FitManager;
import bb.lanxing.lib.devices.sync.FitProcessor;
import bb.lanxing.lib.devices.sync.FitTrans;
import java.io.File;

abstract class FitSyncManager extends AbsSyncManager implements FitManager.FitManagerCallback {
    private FitManager mFitManager;
    private FitProcessor mFitProcesser;

    @Override
    protected boolean doUpload(DeviceFile deviceFile) {
        return false;
    }

    protected boolean isIgnoreCRC() {
        return false;
    }

    protected boolean isIgnoreDecoderError() {
        return false;
    }

    @Override
    public boolean shouldSyncWithServer() {
        return false;
    }

    public FitSyncManager(FitProcessor fitProcessor, String str) {
        init(fitProcessor, str);
    }

    public FitSyncManager() {
        init(null, null);
    }

    public void init(FitProcessor fitProcessor, String str) {
        this.mFitProcesser = fitProcessor;
        this.mFitManager = new FitManager(str, this);
    }

    @Override
    public void release() {
        super.release();
        FitManager fitManager = this.mFitManager;
        if (fitManager != null) {
            fitManager.release();
            this.mFitManager = null;
        }
        this.mFitProcesser = null;
    }

    @Override
    protected boolean exists(long j) {
        FitManager fitManager = this.mFitManager;
        return fitManager != null && fitManager.findFitFile(String.valueOf(j)) != null;
    }

    public void onReceivedFitData(byte[] bArr, boolean z) {
        DeviceFile currentItem = getCurrentItem();
        if (this.mFitManager == null || currentItem == null) {
            return;
        }
        FitTrans fitTrans = FitTrans.get();
        fitTrans.setIgnoreDecoderError(isIgnoreDecoderError());
        fitTrans.setIgnoreCRC(isIgnoreCRC());
        fitTrans.setPlayload(bArr);
        fitTrans.setCompleted(z);
        fitTrans.setId(currentItem.getId());
        this.mFitManager.write(fitTrans);
    }

    public void onFitReceived(FitTrans fitTrans) {
        if (this.mFitManager != null) {
            notifySyncStatus(fitTrans.getId(), 2);
            this.mFitProcesser.onStart(fitTrans);
            this.mFitManager.decode(fitTrans);
            return;
        }
        reset();
        notifySyncStatus(fitTrans.getId(), 4);
    }

    public void onSport(FitTrans fitTrans, SessionMesg sessionMesg) {
        this.mFitProcesser.onSport(sessionMesg);
    }

    public void onMesg(FitTrans fitTrans, Mesg mesg) {
        if (mesg == null) {
            this.mFitProcesser.onComplete(fitTrans);
            if (shouldSyncWithServer()) {
                doUpload(getCurrentItem());
            }
            notifySyncStatus(fitTrans.getId(), 3);
            return;
        }
        this.mFitProcesser.onMesg(mesg);
    }

    public void onError(FitTrans fitTrans, Throwable th) {
        reset();
        notifySyncStatus(fitTrans.getId(), 4);
        if (fitTrans.getPath() != null) {
            File file = new File(fitTrans.getPath());
            if (!file.exists()) {
                return;
            }
            file.delete();
        }
    }

    @Override
    public String getPath(long j) {
        return this.mFitManager.getPath(String.valueOf(j));
    }
}
